package org.colafries.admin.controller;

import com.alibaba.fastjson.JSONObject;
import net.bytebuddy.asm.Advice;
import org.colafries.admin.entity.domain.CustomQueryParam;
import org.colafries.admin.entity.vo.CustomQueryRspVO;
import org.colafries.admin.exceptions.ServiceException;
import org.colafries.admin.exceptions.enums.ErrorCodeEnum;
import org.colafries.admin.wrapper.Response;
import org.colafries.admin.wrapper.ResponseWrapper;
import com.google.common.collect.Maps;
import org.colafries.admin.entity.vo.CustomQueryReqVO;
import org.colafries.admin.service.ICustomQueryService;
import org.colafries.common.utils.excel.DynamicEasyExcelExportUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.*;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @author colafries
 * @version v1.0
 * @ClassName CustomQueryController
 * @Description 自定义查询
 * @date Created in 2024/4/22
 **/
@RestController
@RequestMapping("/customQuery")
public class CustomQueryController {
    @Resource
    ICustomQueryService iCustomQueryService;

    /**
     * 模型自定义查询
     *
     * @param customQueryReqVO 模型请求vo
     * @author colafries
     * @Date 2024/3/26
     */
    @PostMapping("/query")
    public Response<?> query(@RequestBody @Valid CustomQueryReqVO customQueryReqVO) {
        try {
            return iCustomQueryService.query(customQueryReqVO);
        }catch (Exception e){
            throw new ServiceException(ErrorCodeEnum.GL999999,"");
        }
    }

    /**
     * 自定义查询结果导出
     *
     * @param customQueryReqVO
     * @param response
     * @author colafries
     * @Date 2024/5/9
     */
    @PostMapping("/download")
    public Response<?> download(@RequestBody CustomQueryReqVO customQueryReqVO, HttpServletResponse response) throws IOException {
        // 补充导出参数
        customQueryReqVO.setCurrent(1);
        customQueryReqVO.setSize(-1);
        // 查询数据
        Response<?> queryResponse = iCustomQueryService.query(customQueryReqVO);
        CustomQueryRspVO result = (CustomQueryRspVO) queryResponse.getData();
        // head
        LinkedHashMap<String, String> headColumnMap = Maps.newLinkedHashMap();
        result.getColumns().forEach(columnRspVO -> headColumnMap.put(columnRspVO.getFieldHash(), columnRspVO.getAlias()));
        // data
        List<LinkedHashMap<String, Object>> dataList = result.getRows();

        byte[] fileStream = DynamicEasyExcelExportUtils.exportExcelFile(headColumnMap, dataList);
        response.setContentType("application/vnd.ms-excel;charset=utf-8");
        response.getOutputStream().write(fileStream);
        return null;
    }

    /**
     * 保存自定义查询参数
     *
     * @param customQueryReqVO
     * @author colafries
     * @Date 2024/4/14
     */
    @PostMapping("/saveQueryParam")
    public Response<?> saveQueryParam(@RequestBody CustomQueryReqVO customQueryReqVO) {
        boolean result = iCustomQueryService.saveCustomQueryParam(customQueryReqVO);
        return ResponseWrapper.ok(result);
    }

    /**
     * 获取最新保存自定义查询参数
     *
     * @param customQueryReqVO
     * @author colafries
     * @Date 2024/4/14
     */
    @PostMapping("/getLatestQueryParam")
    public Response<?> getLatestQueryParam(@RequestBody CustomQueryReqVO customQueryReqVO) {
        CustomQueryParam result = iCustomQueryService.getLatestCustomQueryParam(customQueryReqVO);
        return ResponseWrapper.ok(result);
    }

    /**
     * 获取所有已保存自定义查询参数
     * @param customQueryReqVO
     * @author colafries
     * @Date 2024/5/20
     */
    @PostMapping("/getAllQueryParam")
    public Response<?> getAllQueryParam(@RequestBody CustomQueryReqVO customQueryReqVO) {
        List<CustomQueryParam> result = iCustomQueryService.getAllQueryParam(customQueryReqVO);
        return ResponseWrapper.ok(result);
    }
}
